//导入vue-router库
import Vue from 'vue'
import Router from 'vue-router'
//导入配置路由的组件
import header from '../components/header/header'
import menu from '../components/header/menu'
import main from '../components/main/main'
import register from '../components/registration/register'
import addCheckApply from '../components/doctor/addCheckApply'
import checkApply from '../components/doctor/checkApply'
import editCheckApply from '../components/doctor/editCheckApply'
import medicalRecord from '../components/doctor/medicalRecord'
import editMedicalRecord from '../components/doctor/editMedicalRecord'
import addPrescription from '../components/doctor/addPrescription'
import prescription from '../components/doctor/prescription'

import register_del from '../components/registration/register_del'
import checkApply_fee from '../components/registration/checkApply_fee'
import register_query from '../components/registration/register_query'
import register_update from '../components/registration/register_update'
import prescription_Fee from '../components/registration/prescription'
import login from '../components/main/login'

import rule from '../components/manage/rule'
import schedul from '../components/manage/schedul'
import dispensing from '../components/drugstore/dispensing'
import  medicineRturn from '../components/drugstore/medicineReturn'
import  medicineManagement from '../components/drugstore/medicineManagement'
import check from '../components/medicalTech/check'
import docReg from '../components/manage/docReg'

import storage from 'good-storage'
Vue.use(Router)
//配置路由
const router =new Router({
    routes: [
        {
            path: '/',
            component: main
        },
        {
            path: '/login',
            component: login
        },
        {
            path: '/main',
            component: main,
            children:[
                {
                    path:'/register',
                    component:register
                },
                {
                    path:'/checkApply',
                    component:checkApply
                },
                {
                    path:'/addCheckApply',
                    component:addCheckApply
                },
                {
                    path:'/editCheckApply/:id',
                    component:editCheckApply
                },
                {
                    path:'/medicalRecord',
                    component:medicalRecord
                },
                {
                    path:'/editMedicalRecord/:id',
                    component:editMedicalRecord
                },
                {
                    path:'/prescription_Fee',
                    component:prescription_Fee
                },
                {
                    path:'/addPrescription',
                    component:addPrescription
                },
                {
                    path: '/prescription',
                    component: prescription
                },
                {
                    path: '/register_update/:id',
                    component: register_update
                },
                {
                    path: '/register',
                    component: register
                },
                {
                    path: '/register_del',
                    component: register_del
                },
                {
                    path: '/checkApply_fee',
                    component: checkApply_fee
                },
                {
                    path: '/register_query',
                    component: register_query
                },
                {
                    path: '/rule',
                    component: rule
                },
                {
                    path: '/docReg',
                    component: docReg
                }
                ,{
                    path: '/schedul',
                    component: schedul
                },
                {
                    path: '/checkApply_fee',
                    component: checkApply_fee
                },
                {
                    path: '/register_query',
                    component: register_query
                },
                {
                    path: '/rule',
                    component: rule
                },
                {
                    path: '/schedul',
                    component: schedul
                },
                {
                    path:'/dispensing',
                    component:dispensing,
                },
                {
                    path:'/medicineReturn',
                    component:medicineRturn,

                },
                {
                    path:'/medicineManagement',
                    component:medicineManagement,
                },
                {
                    path:'/check',
                    component:check,
                },

            ]
        },
        {
            path: '/header',
            component: header
        },
        {
            path: '/menu',
            component: menu
        }
    ]
})
router.beforeEach((to, from, next) => {
    if (to.path.startsWith('/login')) {
        next()

    } else {
        let username = window.localStorage.getItem('username');

        if (!username) {
            next({
                path: '/login'
            })
        } else {
            let user= storage.get("user")
            switch (user.useType) {
                case 0:
                    switch (to.path) {
                        case '/docReg':
                            next()
                            break;
                        case '/rule':
                            next()
                            break;
                        case '/schedul':
                            next()
                            break;
                        default:
                            next(from.path)
                            break;
                    }
                    break;
                case 1:
                    switch (to.path) {
                        case '/dispensing':
                            next()
                            break;
                        case '/medicineManagement':
                            next()
                            break;
                        case '/medicineReturn':
                            next()
                            break;
                        default:
                            next(from.path)
                            break;
                    }
                    break;
                case 2:
                    switch (to.path) {
                        case '/checkApply_fee':
                            next()
                            break;
                        case '/prescription_Fee':
                            next()
                            break;
                        case '/register':
                            next()
                            break;
                        case '/register_del':
                            next()
                            break;
                        case '/register_query':
                            next()
                            break;
                        case '/register_update':
                            next()
                            break;
                        case '/registerSearch':
                            next()
                            break;
                        default:
                            next(from.path)
                            break;
                    }
                    break;
                case 3:
                    switch (to.path) {
                        case '/addCheckApply':
                            next()
                            break;
                        case '/addPrescription':
                            next()
                            break;
                        case '/checkApply':
                            next()
                            break;
                        case '/editCheckApply':
                            next()
                            break;
                        case '/editMedicalRecord':
                            next()
                            break;
                        case '/medicalRecord':
                            next()
                            break;
                        case '/prescription':
                            next()
                            break;
                        case '/check':
                            next()
                            break;
                        default:
                            next(from.path)
                            break;
                    }
                    break;
                case 4:
                    next()
                    break;
            }

        }

    }

})



//导出路由对象
export default  router